/**
 * 
 * @desc  设置localstorage
 * @param {String} key 
 * @param {Any} value
 */
function setItem(key, value) {
    if (!key || (value !== 0 && !value)) return;
    key = key.toString();
    if (typeof value === "object") {
        value = JSON.stringify(value);
    }
    localStorage.setItem(key, value);
}

/**
 * 
 * @desc  获取localstorage
 * @param {String} key 
 * @param {Any} value
 */
function getItem(key, value) {
    if (!key) return;
    key = key.toString();
    var data = localStorage.getItem(key);
    return data ? data : value;
}

/**
 * 
 * @desc  删除localstorage
 * @param {String} key
 */
function removeItem(key) {
    if (!key) return;
    key = key.toString();
    localStorage.removeItem(key);
}

var safeStorage = {
    setItem: setItem,
    getItem: getItem,
    removeItem: removeItem
};

module.exports = safeStorage
